layui.use(['form'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.$;

    window.uuid = function () { 
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function (c) {
            var r =Math.random() * 16 | 0 ,
             v = c == 'x' ? r: (r & 0x3|0x8);
            return v.toString(16);
        })
    }

    window.subdealer = function (querydata) { 
        var confirm = layer.confirm("确认添加此经销商？？",function () {
            for (x in querydata){
                querydata[x].dealer_uid = uuid();
            }
            // console.log("Debug querydata:",querydata);
            $.ajax({
                url:"/Dealer/add",
                type:"post",
                data:JSON.stringify(querydata),
                datatype:"json",
                success:(res)=>{
                    // layer.close(wait);
                    var alert = layer.alert(JSON.stringify(res), {
                        title: '最终的提交信息'
                    }, function () {
    
                        // 关闭alert弹出层
                        layer.close(alert);
    
                        layer.close(confirm);
    
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                },
                error: (res) => { 
                    var alert = layer.alert(JSON.stringify(res), {
                        title: '最终的提交信息'
                    }, function () {
    
                        // 关闭alert弹出层
                        layer.close(alert);
                    });
                }
            })
        })
    }
    
    //监听提交
    form.on('submit(saveBtn)', function (data) {
        subdealer([data.field]);
        return false;
    });

});